

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import edu.ksu.cis.bandera.tool.Tool;
import edu.ksu.cis.bandera.tool.ToolConfigurationView;
import edu.ksu.cis.bandera.tool.ToolIconView;
import edu.ksu.cis.bandera.util.BaseObservable;
import edu.ksu.cis.projects.bogor.ast.FSM;
import edu.ksu.cis.projects.bogor.ast.Location;
import edu.ksu.cis.projects.bogor.ast.System;
import edu.ksu.cis.projects.bogor.ast.SystemMember;
import edu.ksu.cis.projects.bogor.ast.Definition;
import edu.ksu.cis.projects.bogor.ast.Fun;
import edu.ksu.cis.projects.bogor.ast.Global;
import edu.ksu.cis.projects.bogor.ast.Transformation;


public class FSMtool extends BaseObservable implements Tool {

	private System bogorSystem;

	private String outputFileName;

	private static List inputParameterList = new ArrayList(1);
	private static List outputParameterList;
	

	
	public void run() throws Exception {
		if (this.bogorSystem != null) {
			Searcher sr= new Searcher();
			sr.test(this.bogorSystem);
			
			/*Integer num = bogorSystem.getSystemMemberCount();
			java.lang.System.out.println("nums: " + num);
			java.lang.System.out.println("bogorsystem ID: " + bogorSystem.getId());
			Writer writer = new Writer("D:/testcases/bandera/examples/"+"result"+".txt");
			int a=0,b=0,c=0,d=0;
			for(int i = 0; i < num ; i++){
				SystemMember member = bogorSystem.getSystemMember(i);
				if(member  instanceof FSM){
					a++;
					FSM fsm = (FSM)member;
					if(fsm.getMain()){
						if(fsm.getActive()){
							 Location l = fsm.getLocation(0);
							 java.lang.System.out.println("loc: " + l);
							 int count = l.getTransformationCount();
							 for(int i1 = 0; i1 < count; i1++){
								 Transformation temp = l.getTransformation(i1);
								 
//								 java.lang.System.out.println("trans: " + );
							 }
						}
						
					}
				}
				if(member instanceof Definition){
					b++;
				}
				if(member instanceof Fun){
					c++;
				}
				if (member instanceof Global) {
					d++;
				}
			}
			writer.close();
			java.lang.System.out.println("fsm no: " + a);
			java.lang.System.out.println("def no: " + b);
			java.lang.System.out.println("fun no: " + c);
			java.lang.System.out.println("glo no: " + d);*/
			
		} else {
			throw new NullPointerException(
					"Cannot get a BIR System that is null.");
		}
	}


	@Override
	public void quit() throws Exception {
	
	}

	@Override
	public void setInputMap(Map inputMap) throws Exception {
		if (inputMap == null) {
			throw new NullPointerException();
		}

		if (inputMap.containsKey("system")) {
			if (!(inputMap.get("system") instanceof System)) {
				throw new ClassCastException(
						"The variable system is expected to be of type edu.ksu.cis.projects.bogor.ast.System.");
			}
		} else {
			throw new IllegalArgumentException(
					"The mapping with key \"system\" is not present.");
		}

		this.bogorSystem = ((System) inputMap.get("system"));
		
		
	}

	@Override
	public Map getOutputMap() {
		return null;
	}

	@Override
	public List getInputParameterList() {
		return inputParameterList;
	}

	@Override
	public List getOutputParameterList() {
		return outputParameterList;
	}

	@Override
	public void setConfiguration(String configurationString) throws Exception {
		if ((configurationString == null) || (configurationString.length() < 1)) {
			return;
		}

		ByteArrayInputStream bais = new ByteArrayInputStream(
				configurationString.getBytes());
		Properties properties = new Properties();
		properties.load(bais);
		bais.close();
		String temp = properties
				.getProperty("edu.ksu.cis.bandera.tool.birwriter.BIRWriterTool.outputFileName");
		if ((temp != null) && (temp.length() > 0)) {
			this.outputFileName = temp;
		}

	}

	@Override
	public String getConfiguration() throws Exception {
		String configuration = "";

		if ((this.outputFileName != null) && (this.outputFileName.length() > 0)) {
			configuration = "edu.ksu.cis.bandera.tool.birwriter.BIRWriterTool.outputFileName="
					+ this.outputFileName;
		}
		return configuration;
	}

	@Override
	public ToolConfigurationView getToolConfigurationView() {
		return null;
	}

	@Override
	public ToolIconView getToolIconView() {
		return null;
	}
	
	static {
		inputParameterList.add("system");
		outputParameterList = new ArrayList(0);
	}

}
